﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using Smartkernel.Caching;

namespace Smartkernel.NUnitTest.Caching
{
    [Category("Caching")]
    [TestFixture]
    public class SmartMemoryCacherTest
    {
        [Test]
        public void Test()
        {
            //【01】加入默认分类永久缓存项（涉及方法：Add、GetValue、Clean、CleanAll、Remove、ContainsKey）
            long ticks1 = DateTime.Now.Ticks;
            SmartMemoryCacher.Add("TIME1", ticks1);
            SmartMemoryCacher.Clean();
            SmartMemoryCacher.CleanAll();
            Assert.AreEqual((long)SmartMemoryCacher.GetValue("TIME1"), ticks1);
            SmartMemoryCacher.Remove("TIME1");
            Assert.AreEqual(SmartMemoryCacher.ContainsKey("TIME1"), false);

            //【02】加入指定分类永久缓存项（涉及方法：Add、GetValue、Clean、CleanAll、Remove、ContainsKey）
            long ticks2 = DateTime.Now.Ticks;
            SmartMemoryCacher.Add("SmartMemoryCacherTest2", "TIME2", ticks2);
            SmartMemoryCacher.Clean("SmartMemoryCacherTest2");
            SmartMemoryCacher.CleanAll();
            Assert.AreEqual((long)SmartMemoryCacher.GetValue("SmartMemoryCacherTest2", "TIME2"), ticks2);
            SmartMemoryCacher.Remove("SmartMemoryCacherTest2", "TIME2");
            Assert.AreEqual(SmartMemoryCacher.ContainsKey("SmartMemoryCacherTest2", "TIME2"), false);

            //【03】加入默认分类按时间条件超时的缓存项（涉及方法：Add、ContainsKey、Clean）
            long ticks3 = DateTime.Now.Ticks;
            SmartMemoryCacher.Add("TIME3", ticks3, 1, true);
            Assert.AreEqual(SmartMemoryCacher.ContainsKey("TIME3"), true);
            System.Threading.Thread.Sleep(1100);
            SmartMemoryCacher.Clean();
            Assert.AreEqual(SmartMemoryCacher.ContainsKey("TIME3"), false);

            long ticks4 = DateTime.Now.Ticks;
            SmartMemoryCacher.Add("TIME4", ticks4, 1, true);
            Assert.AreEqual(SmartMemoryCacher.ContainsKey("TIME4"), true);
            System.Threading.Thread.Sleep(1100);
            ticks4 = (long)SmartMemoryCacher.GetValue("TIME4");
            SmartMemoryCacher.Clean();
            Assert.AreEqual(SmartMemoryCacher.ContainsKey("TIME4"), true);

            long ticks5 = DateTime.Now.Ticks;
            SmartMemoryCacher.Add("TIME5", ticks5, 1, false);
            Assert.AreEqual(SmartMemoryCacher.ContainsKey("TIME5"), true);
            System.Threading.Thread.Sleep(1100);
            ticks5 = (long)SmartMemoryCacher.GetValue("TIME5");
            SmartMemoryCacher.Clean();
            Assert.AreEqual(SmartMemoryCacher.ContainsKey("TIME5"), false);

            //【04】加入指定分类按时间条件超时的缓存项（涉及方法：Add、ContainsKey、Clean）
            long ticks6 = DateTime.Now.Ticks;
            SmartMemoryCacher.Add("SmartMemoryCacherTest3", "TIME6", ticks6, 1, true);
            Assert.AreEqual(SmartMemoryCacher.ContainsKey("SmartMemoryCacherTest3", "TIME6"), true);
            System.Threading.Thread.Sleep(1100);
            SmartMemoryCacher.Clean("SmartMemoryCacherTest3");
            Assert.AreEqual(SmartMemoryCacher.ContainsKey("SmartMemoryCacherTest3", "TIME6"), false);

            long ticks7 = DateTime.Now.Ticks;
            SmartMemoryCacher.Add("SmartMemoryCacherTest3", "TIME7", ticks7, 1, true);
            Assert.AreEqual(SmartMemoryCacher.ContainsKey("SmartMemoryCacherTest3", "TIME7"), true);
            System.Threading.Thread.Sleep(1100);
            ticks7 = (long)SmartMemoryCacher.GetValue("SmartMemoryCacherTest3", "TIME7");
            SmartMemoryCacher.Clean("SmartMemoryCacherTest3");
            Assert.AreEqual(SmartMemoryCacher.ContainsKey("SmartMemoryCacherTest3", "TIME7"), true);

            long ticks8 = DateTime.Now.Ticks;
            SmartMemoryCacher.Add("SmartMemoryCacherTest3", "TIME8", ticks8, 1, false);
            Assert.AreEqual(SmartMemoryCacher.ContainsKey("SmartMemoryCacherTest3", "TIME8"), true);
            System.Threading.Thread.Sleep(1100);
            ticks8 = (long)SmartMemoryCacher.GetValue("SmartMemoryCacherTest3", "TIME8");
            SmartMemoryCacher.Clean("SmartMemoryCacherTest3");
            Assert.AreEqual(SmartMemoryCacher.ContainsKey("SmartMemoryCacherTest3", "TIME8"), false);

            //【05】加入默认分类按时间和次数超时的缓存项（涉及方法：Add、CleanAll、ContainsKey、GetValue）
            long ticks9 = DateTime.Now.Ticks;
            SmartMemoryCacher.Add("TIME9", ticks9, 100, true, 10);
            SmartMemoryCacher.CleanAll();
            Assert.AreEqual(SmartMemoryCacher.ContainsKey("TIME9"), false);

            long ticks10 = DateTime.Now.Ticks;
            SmartMemoryCacher.Add("TIME10", ticks9, 100, true, 10);
            for (int i = 0; i < 15; i++)
            {
                ticks10 = (long)SmartMemoryCacher.GetValue("TIME10");
            }
            SmartMemoryCacher.CleanAll();
            Assert.AreEqual(SmartMemoryCacher.ContainsKey("TIME10"), true);

            //【06】加入指定分类按时间和次数超时的缓存项（涉及方法：Add、CleanAll、ContainsKey、GetValue）
            long ticks11 = DateTime.Now.Ticks;
            SmartMemoryCacher.Add("SmartMemoryCacherTest4", "TIME11", ticks11, 100, true, 10);
            SmartMemoryCacher.CleanAll();
            Assert.AreEqual(SmartMemoryCacher.ContainsKey("SmartMemoryCacherTest4", "TIME11"), false);

            long ticks12 = DateTime.Now.Ticks;
            SmartMemoryCacher.Add("SmartMemoryCacherTest4", "TIME12", ticks12, 100, true, 10);
            for (int i = 0; i < 15; i++)
            {
                ticks12 = (long)SmartMemoryCacher.GetValue("SmartMemoryCacherTest4", "TIME12");
            }
            SmartMemoryCacher.CleanAll();
            Assert.AreEqual(SmartMemoryCacher.ContainsKey("SmartMemoryCacherTest4", "TIME12"), true);

            //【07】清空缓存项（涉及方法：Add、Clear、ContainsKey、ClearAll）
            long ticks13 = DateTime.Now.Ticks;
            SmartMemoryCacher.Add("SmartMemoryCacherTest5", "TIME13", ticks13, 1000, true);
            SmartMemoryCacher.Clear("SmartMemoryCacherTest5");
            Assert.AreEqual(SmartMemoryCacher.ContainsKey("SmartMemoryCacherTest5", "TIME13"), false);

            long ticks14 = DateTime.Now.Ticks;
            SmartMemoryCacher.Add("TIME14", ticks14, 1000, true);
            SmartMemoryCacher.Clear();
            Assert.AreEqual(SmartMemoryCacher.ContainsKey("TIME14"), false);

            long ticks15 = DateTime.Now.Ticks;
            SmartMemoryCacher.Add("SmartMemoryCacherTest6", "TIME15", ticks15, 1000, true);
            SmartMemoryCacher.ClearAll();
            Assert.AreEqual(SmartMemoryCacher.ContainsKey("SmartMemoryCacherTest6", "TIME15"), false);

            long ticks16 = DateTime.Now.Ticks;
            SmartMemoryCacher.Add("TIME16", ticks16, 1000, true);
            SmartMemoryCacher.ClearAll();
            Assert.AreEqual(SmartMemoryCacher.ContainsKey("TIME16"), false);
        }
    }
}
